package com.cards_game.simple_ddz.handler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cards_game.simple_ddz.bean.BaseCardBean;
import com.cards_game.simple_ddz.bean.ResultBean;
import com.cards_game.simple_ddz.bean.SimpleDdzBean;
import com.cards_game.simple_ddz.bean.UserBean;
import com.cards_game.simple_ddz.service.PlayGameService;

/**
 * 
 * @author 郭国辉
 * 
 *     2017年6月30日下午3:38:20
 */
@Controller
public class PlayGameHandler {
	@Resource
	private PlayGameService playGameService;
	/**
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="discardCards.do", produces = "text/html;charset=UTF-8")
	public boolean discardCards(HttpServletRequest request, ModelMap modelMap){
		List<BaseCardBean> cbs=new ArrayList<>();
		boolean res=playGameService.discardCards(cbs);
		return res;
	}
	
	/**
	 * 获取当前房间状况，房间状态一共分：
	 *  1、全部未准备
	 *  2、一个准备
	 *  3、两个准备，可以开始计时，30秒后将未准备玩家踢出房间
	 *  4、三个准备，开始发牌
	 *  5、发牌完毕，抢地主阶段
	 *  6、地主确定，亮底牌
	 *  7、出牌阶段
	 *  8、一人牌出完，结束阶段
	 * @param request
	 * @param modelMap
	 * @return
	 */
	public ResultBean<?> getNowCondition(HttpServletRequest request, ModelMap modelMap){
		UserBean ub=(UserBean) request.getSession().getAttribute("userBean");
		int userId=ub.getId();
		
		ResultBean<?> res=playGameService.getNowCondition(userId);
		return res;
	}
	
	/**
	 * 发牌，获取纸牌
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getSimpleDdzCards.do", produces = "text/html;charset=UTF-8")
	public List<SimpleDdzBean> getSimpleDdzCards(HttpServletRequest request, ModelMap modelMap) {
		UserBean ub=(UserBean) request.getSession().getAttribute("userBean");
		int userId=ub.getId();
		List<SimpleDdzBean> list = playGameService.getSimpleDdzCards(userId);
		return list;
	}
	/**
	 * 发牌，获取底牌
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getBottomCards.do", produces = "text/html;charset=UTF-8")
	public List<SimpleDdzBean> getBottomCards(HttpServletRequest request, ModelMap modelMap) {
		UserBean ub=(UserBean) request.getSession().getAttribute("userBean");
		int userId=ub.getId();
		List<SimpleDdzBean> list = playGameService.getBottomCards(userId);
		return list;
	}
	/**
	 * 发牌，获取上家下家纸牌张数
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getPartner.do", produces = "text/html;charset=UTF-8")
	public Map<Integer,Integer> getPartner(HttpServletRequest request, ModelMap modelMap) {
		UserBean ub=(UserBean) request.getSession().getAttribute("userBean");
		int userId=ub.getId();
		Map<Integer,Integer> map = playGameService.getPartner(userId);
		return map;
	}
}
